package com.iyao_app.mvp.info;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.iyao_app.R;
import com.iyao_app.adapter.WhoCallMeAdapter;
import com.iyao_app.base.BaseActivityWithHead;
import com.iyao_app.entry.CallList;
import com.iyao_app.entry.HomeData;
import com.iyao_app.entry.PageInfo;
import com.iyao_app.entry.ResultModel;
import com.iyao_app.http.NetCallBack;
import com.iyao_app.http.NetUtils;
import com.iyao_app.mvp.other.OtherHomeActivity;
import com.iyao_app.util.Contants;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/12/4.
 */

public class WhoCallMeActivity extends BaseActivityWithHead implements OnRefreshLoadmoreListener {

    @BindView(R.id.rl_empty)
    RelativeLayout mRlEmpty;
    @BindView(R.id.x_recyclerView)
    RecyclerView mXRecyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    private WhoCallMeAdapter adapter;
    private PageInfo pageInfo;
    private int page = 1;
    private final int pageSize = 10;
    private String id;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_who_call_me);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        id = getIntent().getStringExtra(Contants.TYPE);
        int type = getIntent().getIntExtra(Contants.CLASS_TYPE,0);
        if (type==1){
            setTitleTV(getString(R.string.who_call_me));
        }else {
            setTitleTV(getString(R.string.who_call_ta));
        }

        adapter = new WhoCallMeAdapter(null);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        adapter.isFirstOnly(true);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mXRecyclerView.setAdapter(adapter);
        mXRecyclerView.setLayoutManager(layoutManager);
        mRefreshLayout.setOnRefreshLoadmoreListener(this);
        mRefreshLayout.autoRefresh();
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.sdv_head) {
                    CallList item = (CallList) adapter.getItem(position);
                    Intent intent = new Intent(WhoCallMeActivity.this, OtherHomeActivity.class);
                    intent.putExtra(Contants.TYPE, String.valueOf(item.sayHelloFromUserId));
                    startActivity(intent);
                }
            }
        });
    }

    private void callList(final boolean isRefresh) {
        if (TextUtils.isEmpty(id)) {
            return;
        }
        if (isRefresh) {
            page = 1;
        } else {
            page++;
        }
        NetUtils.getInstance().sayHelloList(id, page, pageSize, new NetCallBack() {
            @Override
            public void onSuccess(String message, int code, String response, ResultModel model) {
                if (code == Contants.SUCCESS_CODE) {
                    if (model != null) {
                        HomeData homeData = model.getModel();
                        if (homeData != null) {
                            List<CallList> list = homeData.getList();
                            pageInfo = homeData.getPageInfo();
                            if (isRefresh) {
                                adapter.getData().clear();
                                adapter.notifyDataSetChanged();
                                mRefreshLayout.finishRefresh();
                            } else {
                                mRefreshLayout.finishLoadmore();
                            }
                            if (pageInfo.pageTotal == page) {
                                mRefreshLayout.setEnableLoadmore(false);
                                mRefreshLayout.finishLoadmoreWithNoMoreData();
                            } else {
                                mRefreshLayout.setEnableLoadmore(true);
                            }
                            adapter.addData(list);
                        }
                    }
                }
            }

            @Override
            public void onFail(String message, int code) {

            }
        }, HomeData.class);
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        callList(false);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        callList(true);
    }
}
